package br.com.bolsamovel;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListAdapter;

public class UIUtils {
	
	public static double asDouble(EditText et) throws Exception {
		return StringUtils.normalizeAsNumber(et.getText().toString());
	}

	public static String asString(EditText et) throws Exception {
		return et.getText().toString();
	}

	public static int asDate(EditText et) throws Exception {
		String value = et.getText().toString();
		String[] items = value.split("/");

		int day = Integer.parseInt(items[0].trim());
		int month = Integer.parseInt(items[1].trim());
		int year = Integer.parseInt(items[2].trim());

		return (year * 10000) + (month * 100) + day;
	}

	public static void set(EditText et, String value) {
		value = value == null ? "" : value;
		
		et.getText().clear();
		
		if(et instanceof NumberEdit){
			((NumberEdit)et).initText(value);
		}else{
			et.getText().append(value == null ? "" : value);
		}
	}

	public static void doConfirmedJob(int msg, Context c, final Runnable job) {
		doConfirmedJob(c.getResources().getString(msg), c, job);
	}
	
	public static void doConfirmedJob(CharSequence msg, Context c, final Runnable job) {
		AlertDialog.Builder builder = new AlertDialog.Builder(c);
		builder.setMessage(msg).setCancelable(false).setPositiveButton(R.string.str_yes, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				job.run();
			}
		}).setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});

		AlertDialog alert = builder.create();
		alert.show();
	}

	public static void showDatePicker(Context ctx, final EditText edit) {
		String value = edit.getText().toString();

		int year = 0;
		int month = 0;
		int day = 0;

		if (value != null && value.trim().length() > 0) {
			String[] items = value.split("/");
			day = Integer.parseInt(items[0].trim());
			month = Integer.parseInt(items[1].trim()) - 1;
			year = Integer.parseInt(items[2].trim());
		} else {
			Calendar c = new GregorianCalendar();
			year = c.get(Calendar.YEAR);
			month = c.get(Calendar.MONTH);
			day = c.get(Calendar.DAY_OF_MONTH);
		}

		Dialog dlg = new DatePickerDialog(ctx, new DatePickerDialog.OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				StringBuffer buf = new StringBuffer();
				String strDate = buf.append(dayOfMonth).append("/").append(monthOfYear + 1).append("/").append(year).toString();
				set(edit, strDate);
			}
		}, year, month, day);

		dlg.show();
	}

	public static void showErrorMessage(Context ctx, Throwable e) {
		e.printStackTrace();
		
		new AlertDialog.Builder(ctx)

		.setTitle("Erro !")

		.setMessage(e.getMessage())

		.setNeutralButton("Ok",

		new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog,	int which) {
				dialog.cancel();
			}

		}).show();
	}
	
	public static void throwsNonChecked(Throwable e){
		IllegalStateException error = new IllegalStateException();
		error.initCause(e);
		throw error;
	}
}
